﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gedea.WinForm.Helpers;
using Gedea.Service.Services;
using Gedea.Model.Entities;

namespace Gedea.WinForm.Forms.FileText
{
    public partial class Generate : Form
    {
        static List<Customer> customerList = new List<Customer>();

        #region Ctr
        public static Generate Instance
        {
            get { return ViewLocator<Generate>.Instance; }
        }

        private Generate()
        {
            InitializeComponent();
        }
        #endregion

        #region Load

        private void Generate_Load(object sender, EventArgs e)
        {
            ComboBox1_Load();

            ValidateButton();

            DataGridView1_Load();

            DateTimePicker1_Load();

            textBox2.Text = "00";

            customerList = new List<Customer>();
        }

        #endregion

        #region Actions

        private void Generate_FormClosed(object sender, FormClosedEventArgs e)
        {
            ViewLocator<Generate>.CloseInstance = null;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            ValidateKeyPressNumber(e);
            ValitateExtention(e, 8);
        }


        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            ValidateKeyPressNumber(e);
            ValitateExtention(e, 2);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ValidateButton();

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            ValidateButton();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Customer cust = createCustomer();

            customerList.Add(cust);

            dataGridView1.Rows.Add(cust.RealName, cust.Amount, cust.ExpirationDate, "Borrar");
            comboBox1.SelectedIndex = 0;
            textBox1.ResetText();
            dateTimePicker1.ResetText();
            textBox2.Text = "00";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            new TextFileService().Generate(customerList);
            MessageBox.Show("El listado se ha generado con éxito.");
            this.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].CellType.Name == new DataGridViewButtonColumn().CellType.Name)
            {
                if (dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString() == "Borrar")
                {
                    customerList.Remove(customerList[e.RowIndex]);
                    dataGridView1.Rows.RemoveAt(e.RowIndex);
                    dataGridView1.Refresh();
                }
            }
        }

        #endregion

        #region Private

        private void ValidateButton()
        {
            button1.Enabled = (textBox1.Text != "" && comboBox1.SelectedValue.ToString() != "0" && textBox2.Text != "");
        }

        private Customer createCustomer()
        {
            Customer res = new Customer();
            res = new CustomerService().Get(Convert.ToInt32(comboBox1.SelectedValue));
            res.ExpirationDate = Convert.ToDateTime(dateTimePicker1.Text);
            res.Amount = Convert.ToDecimal(textBox1.Text + textBox2.Text);
            return res;
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            ValidateButton();
        }

        private void DataGridView1_Load()
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.Columns.Add(CreateColumn("Nombre Completo", "RealName", 300));
            dataGridView1.Columns.Add(CreateColumn("Monto", "Amount", 150));
            dataGridView1.Columns.Add(CreateColumn("Fecha de Vencimiento", "DateTime", 200));
            dataGridView1.Columns.Add(new DataGridViewButtonColumn());
        }

        private DataGridViewColumn CreateColumn(string header, string prop, int width)
        {
            DataGridViewColumn res = new DataGridViewTextBoxColumn();
            res.HeaderText = header;
            res.DataPropertyName = prop;
            res.Width = width;

            return res;
        }

        private void ComboBox1_Load()
        {
            List<Customer> dataBind = new List<Customer>();

            Customer defaultCust = new Customer();
            defaultCust.RealName = "Seleccione un cliente";
            defaultCust.CustomerId = 0;
            dataBind.Add(defaultCust);
            dataBind.AddRange(new CustomerService().List());

            comboBox1.Items.Insert(0, defaultCust);
            comboBox1.DataSource = dataBind;
            comboBox1.DisplayMember = "RealName";
            comboBox1.ValueMember = "CustomerId";

        }

        private void DateTimePicker1_Load()
        {
            dateTimePicker1.MinDate = DateTime.Now;
            dateTimePicker1.MaxDate = DateTime.Now.AddMonths(6);
        }

        private static void ValidateKeyPressNumber(KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) || char.IsWhiteSpace(e.KeyChar) || char.IsSymbol(e.KeyChar))
            {
                MessageBox.Show("El importe debe ser numérico");
                e.Handled = true;
                return;
            }
        }

        private void ValitateExtention(KeyPressEventArgs e, int length)
        {
            //if ((char.IsPunctuation(e.KeyChar) || char.IsSymbol(e.KeyChar) || char.IsSeparator(e.KeyChar) || char.IsWhiteSpace(e.KeyChar))
            //    || (this.ActiveControl.Text.Length > 3 && char.IsControl(e.KeyChar)))
            if (this.ActiveControl.Text.Length > length - 1 && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                return;
            }
        }

        #endregion

    }
}
